﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using Public;
namespace QuanLyKhoHangVatTu
{
    public partial class DonVi : DevExpress.XtraEditors.XtraForm
    {
        DonVi_PB dv_pb = new DonVi_PB();
        DonVi_BUS dv_bus = new DonVi_BUS();
        public DonVi()
        {
            InitializeComponent();
        }
        public DonVi(string MaDonViTinh, string DienGiai, string Diachi, string CapToChuc)
        {
            InitializeComponent();

            txtMa.Text = MaDonViTinh;
            txtTen.Text = DienGiai;
            txtDiaChi.Text = Diachi;
            txtCapToChuc.Text = CapToChuc;
            txtMa.Properties.ReadOnly = true;
        }

        public DonVi_PB GetValues()
        {
            DonVi_PB.MaDonVi = txtMa.Text;
            DonVi_PB.TenDonVi = txtTen.Text;
            DonVi_PB.DiaChi = txtDiaChi.Text;
            DonVi_PB.CapToChuc = txtCapToChuc.Text;
            return dv_pb;
        }

        private bool CheckData()
        {
            if (txtMa.Text == string.Empty)
            {
                txtMa.ErrorText = "Mã không được trống";
                return false;
            }
            if (txtTen.Text == string.Empty)
            {
                txtTen.ErrorText = "Tên không được trống";
                return false;
            }
            // ktra trung ma
            if (dv_bus.Exists(txtMa.Text))
            {
                if (Flag_PB.Sua == true)
                {
                    Flag_PB.Sua = false;
                    return true;

                }
                txtMa.ErrorText = "Trùng mã";
                return false;
            }

            return true;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            GetValues();
            if (CheckData())
            {
                try
                {
                    dv_bus.ThemSuaDonVi(dv_pb);
                }
                catch(Exception ex)
                {
                   // MessageBox.Show("Mã <" + txtMa.Text + "> đã tồn tại. Xin vui lòng kiểm tra lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    if (ex.Message.Contains(" data would be truncated"))
                        XtraMessageBox.Show("Vượt quá ký tự cho phép", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    
                    return;
                } 

                this.Close();
            }
        }

        private void btnLuu_Them_Click(object sender, EventArgs e)
        {
            txtMa.Properties.ReadOnly = false;
            GetValues();
            if (CheckData())
            {
                
                try
                {
                    dv_bus.ThemSuaDonVi(dv_pb);

                    if (this.Tag != null)
                    {
                        IChildForm iChildForm = this.Tag as IChildForm;
                        if (iChildForm != null)
                            iChildForm.Nap();
                    }
                    txtMa.ResetText();
                    txtTen.ResetText();
                    txtDiaChi.ResetText();
                    txtCapToChuc.ResetText();
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains(" data would be truncated"))
                        XtraMessageBox.Show("Vượt quá ký tự cho phép", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                    return;
                }
                

            }
            
        }

        private void btnHuyBo_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void DonVi_Load(object sender, EventArgs e)
        {
         
        }
    }
}